//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by 胡阳 on 16/5/24.
//  Copyright © 2016年 young4ever. All rights reserved.
//

import UIKit

private let reuseIdentifier = "NewFeatureCollectionCell"

class NewFeatureViewController: UICollectionViewController {
    
    private var layout = NewFeatureLayout()
    
    private let pageCount = 4
    
    init(){
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        
    }
    
    
    
    // MARK: - UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCollectionCell
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last!
        
        if path.item == (pageCount - 1) {
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCollectionCell
            
            cell.startButtonAnimation()
        }
    }
}

// 在swift中,一个文件里面可以定义多个类
// 如果当前类需要监听点击事件 , 那么不能使用"private"修饰
class NewFeatureCollectionCell : UICollectionViewCell
{
    // 在swift中,被"private"修饰的变量如果是在同一个文件中是可以访问的 , 不同的文件不能访问
    private var imageIndex : Int? {
        didSet{
        
            imgView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }
    
    func startButtonAnimation()
    {
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue:0), animations: {
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: { (_) in
                self.startButton.userInteractionEnabled = true
        })
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //初始化子视图
        initSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var imgView = UIImageView()
    private lazy var startButton: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named:"new_feature_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: #selector(NewFeatureCollectionCell.startButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        btn.hidden = true
        return btn
    }()
    
    func startButtonClick(){
        // 去主页
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: true)
    }
    
    private func initSubviews()
    {
        contentView.addSubview(imgView)
        imgView.xmg_Fill(contentView)
        
        contentView.addSubview(startButton)
        startButton.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0.0, y: -80.0))
    }
}

private class NewFeatureLayout: UICollectionViewFlowLayout
{
    override func prepareLayout() {
        super.prepareLayout()
        
        // 1.设置layout
        itemSize = UIScreen.mainScreen().bounds.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 2.设置collectionView的属性
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }
}
